1 00:00:01,380 --> 00:00:01,860 All right. 2 00:00:01,860 --> 00:00:02,340 Welcome. 3 00:00:02,340 --> 00:00:07,170 In this lecture, I'm going to show you how to create a simple day and night cycling system where you 4 00:00:07,170 --> 00:00:11,430 can adjust the duration of the day into how many seconds you'd like the day to last. 5 00:00:11,460 --> 00:00:12,840 This is going to be pretty simple. 6 00:00:12,840 --> 00:00:16,320 All we need to do is create a new script in server script service. 7 00:00:16,770 --> 00:00:20,520 I'm just going to name this script day night cycle. 8 00:00:21,120 --> 00:00:23,400 And in here, we're going to need a few things. 9 00:00:23,400 --> 00:00:26,010 We're going to need access to some services. 10 00:00:26,130 --> 00:00:29,010 We're going to need some constants. 11 00:00:29,940 --> 00:00:31,890 We'll need a variable. 12 00:00:32,610 --> 00:00:37,110 And then we're going to need a listener to listen to an event. 13 00:00:37,110 --> 00:00:38,790 So we'll need an event handler. 14 00:00:39,150 --> 00:00:43,110 Now, the very first service we need to get is the lighting service. 15 00:00:44,760 --> 00:00:47,010 So we'll grab it here in this variable. 16 00:00:47,010 --> 00:00:51,090 And then we're also going to need another service called the run service. 17 00:00:52,710 --> 00:00:53,760 Run service. 18 00:00:54,240 --> 00:00:57,180 After that, we're going to define a few constants here. 19 00:00:57,180 --> 00:01:00,420 One is going to be called day length. 20 00:01:01,210 --> 00:01:04,630 And this is going to represent how long we need the day to be in seconds. 21 00:01:04,630 --> 00:01:10,930 So I'll just set it to 240 seconds and then we'll need a constant for what? 22 00:01:10,930 --> 00:01:13,240 The hour we'd like to start the day at. 23 00:01:13,240 --> 00:01:14,800 So start hour. 24 00:01:15,550 --> 00:01:22,720 And we could set it to be something like maybe 4 a.m. We're going to need to define a constant called 25 00:01:22,720 --> 00:01:24,640 max brightness. 26 00:01:25,720 --> 00:01:31,270 And I'm going to set it to three because in the lighting service there is a brightness property here 27 00:01:31,270 --> 00:01:34,840 and we're going to be adjusting this throughout the day. 28 00:01:34,840 --> 00:01:39,350 So when it's in the morning and in the middle of the day and the afternoon, it'll be at three. 29 00:01:39,370 --> 00:01:42,460 And then when it's at nighttime in the evening, we'll set it to zero. 30 00:01:44,670 --> 00:01:50,640 And then the last thing we need is something called brightness delta. 31 00:01:51,000 --> 00:01:56,580 And we're going to get this by dividing the max brightness by our day length. 32 00:01:56,700 --> 00:02:02,520 And this is because we're going to be accessing an event in the run service called Heartbeat, which 33 00:02:02,550 --> 00:02:08,790 fires multiple times per second, and we're going to be incrementing and decrementing the brightness 34 00:02:08,790 --> 00:02:14,520 property over time as our time elapses and we're going to add and subtract to it with this brightness 35 00:02:14,520 --> 00:02:21,840 delta, which I'm just dividing by our day length to get, you know, kind of a general delta for subtracting 36 00:02:21,840 --> 00:02:22,650 the brightness. 37 00:02:22,680 --> 00:02:27,750 Because let's say if I define a constant brightness delta, if I went and changed the day length to 38 00:02:27,750 --> 00:02:33,570 something like maybe ten seconds and I had a constant brightness delta here, well, we might not be 39 00:02:33,570 --> 00:02:39,360 able to reach our brightness all the way at three or all the way down to zero based on whatever I have 40 00:02:39,360 --> 00:02:40,230 defined in here. 41 00:02:40,230 --> 00:02:47,530 Or maybe if I have this set at like maybe 10,000 seconds, then in the brightness delta might be too 42 00:02:47,530 --> 00:02:53,350 large of a number and it might shift the brightness too quickly while it's still sunny or while it's 43 00:02:53,350 --> 00:02:54,850 still nighttime out or whatever. 44 00:02:54,850 --> 00:03:00,910 So I'm just dividing max brightness by the day length to just kind of get a general number we can use 45 00:03:00,910 --> 00:03:03,760 that'll be proportional to our day length. 46 00:03:04,270 --> 00:03:06,040 And I'm going to need a variable here. 47 00:03:06,040 --> 00:03:07,270 I'll call it start. 48 00:03:07,390 --> 00:03:14,170 And it's going to be equal to we're going to get the tick function and we're going to subtract from 49 00:03:14,170 --> 00:03:14,500 it. 50 00:03:14,530 --> 00:03:22,000 We're going to get the start our we're going to divide the start our by 24 to give us a fraction. 51 00:03:22,000 --> 00:03:25,750 And then we're going to multiply this by the day length. 52 00:03:25,750 --> 00:03:29,140 So what I'm doing here is I'm getting what fraction? 53 00:03:29,990 --> 00:03:30,350 Right. 54 00:03:30,350 --> 00:03:33,920 What percentage of our day length is equal to what? 55 00:03:33,920 --> 00:03:34,520 Of our star? 56 00:03:34,550 --> 00:03:35,840 Our is so our star. 57 00:03:35,870 --> 00:03:38,540 Our is four divided by 24. 58 00:03:38,540 --> 00:03:39,320 So what's four? 59 00:03:39,320 --> 00:03:41,690 24th of our day length. 60 00:03:41,780 --> 00:03:47,930 We get that number and then we subtract it from the tick and we're going to be using Tick to keep track 61 00:03:47,930 --> 00:03:49,670 of how much time has elapsed. 62 00:03:50,910 --> 00:03:57,390 Next, we're going to get our own service and we're going to listen to the Heartbeat event and we'll 63 00:03:57,390 --> 00:03:59,160 connect a function to this. 64 00:03:59,730 --> 00:04:04,050 And in here, we're going to create another two variables. 65 00:04:04,050 --> 00:04:11,430 One, we'll call time elapsed, and this one's going to be equal to the current tick subtracted by our 66 00:04:11,430 --> 00:04:12,240 start time. 67 00:04:13,180 --> 00:04:19,180 So as the time goes on, since Tick returns a number along with decimals, we'll be able to see exactly 68 00:04:19,180 --> 00:04:22,510 how many seconds has elapsed every time this event gets fired. 69 00:04:22,510 --> 00:04:27,120 And we know that because we're subtracting the start time that we made here. 70 00:04:27,130 --> 00:04:31,000 So it'll give us the exact number of how many seconds elapsed. 71 00:04:31,030 --> 00:04:37,810 So that way, you know, since our start hour is at four, we know that this would return 40 seconds 72 00:04:37,810 --> 00:04:39,460 out of 240. 73 00:04:39,460 --> 00:04:42,490 So this will give us a difference of 40 seconds. 74 00:04:42,490 --> 00:04:45,760 So this is telling us, hey, 40 seconds has elapsed so far. 75 00:04:46,660 --> 00:04:50,020 Now, the problem with this is that we don't need seconds. 76 00:04:50,050 --> 00:04:56,770 We need hours because in the lighting service, we need to be able to set the clock time property to 77 00:04:56,770 --> 00:04:58,690 a number that represents hours. 78 00:04:58,690 --> 00:05:01,290 So how do we convert these seconds to hours? 79 00:05:01,300 --> 00:05:09,010 Well, we can create another variable, we'll call it hour of day, and it's going to be equal to we 80 00:05:09,010 --> 00:05:16,450 are going to get the current time elapsed and we're going to divide it by the day length to give us 81 00:05:16,450 --> 00:05:17,590 a fraction. 82 00:05:17,590 --> 00:05:21,100 And then we're just going to multiply that by 24. 83 00:05:21,870 --> 00:05:29,460 So if the time elapsed is 40 seconds, well, 40 divided by 240 gives us a percentage of that day and 84 00:05:29,460 --> 00:05:37,380 we just multiply that to 24 to give us the exact number or percentage that represents that in 24 hours. 85 00:05:37,380 --> 00:05:39,270 So we get the correct hour of day. 86 00:05:39,860 --> 00:05:44,690 Now that we have done that, we're going to create a third variable in here. 87 00:05:44,720 --> 00:05:51,740 I'll just call it target brightness, and I'm not going to initialize anything or assign a value yet 88 00:05:51,740 --> 00:05:58,820 because I want to check if the hour of day is greater than, let's say, seven. 89 00:05:58,820 --> 00:06:00,350 So seven in the morning. 90 00:06:00,500 --> 00:06:10,910 And we also want to make sure the hour of day is less than or equal to 17, which is about 5:00 or 5:00 91 00:06:10,910 --> 00:06:12,320 pm in the afternoon. 92 00:06:12,440 --> 00:06:17,750 If it is within that time range, then we want to set the target brightness to three because it's in 93 00:06:17,750 --> 00:06:18,680 the middle of the day. 94 00:06:19,340 --> 00:06:24,800 Otherwise, if it's not within that time frame, it's either night time or it's in the evening or it's 95 00:06:24,800 --> 00:06:26,150 in the very early morning. 96 00:06:26,180 --> 00:06:29,390 That means we want to set the target brightness to zero. 97 00:06:30,170 --> 00:06:37,550 Now that we have calculated our target brightness, then we can go down here and check if the lighting 98 00:06:37,550 --> 00:06:43,080 service dot brightness, if it's less than our target brightness. 99 00:06:43,080 --> 00:06:47,760 So let's say our target brightness is three and the brightness is zero, then we want to increase the 100 00:06:47,760 --> 00:06:48,660 brightness, right? 101 00:06:48,780 --> 00:06:50,340 So we'll do lighting service. 102 00:06:50,340 --> 00:06:57,990 Dot brightness is going to be equal to and we're going to use the math dot minimum function. 103 00:06:57,990 --> 00:07:00,400 And this function returns a number. 104 00:07:00,420 --> 00:07:06,270 You give it a bunch of numbers and it'll return to you the number that is least out of all those numbers. 105 00:07:06,420 --> 00:07:10,770 So we want to pass our target brightness because that's the max amount of brightness we want. 106 00:07:10,800 --> 00:07:12,180 We don't want to go over that. 107 00:07:13,080 --> 00:07:19,590 And then we're going to pass the lighting service dot brightness, plus this brightness delta. 108 00:07:19,830 --> 00:07:25,140 So every time this function keeps getting run, we're going to keep incrementing the brightness of our 109 00:07:25,140 --> 00:07:30,090 lighting service until we reach this target brightness. 110 00:07:30,270 --> 00:07:33,090 We'll return that and we'll set that brightness to three. 111 00:07:33,090 --> 00:07:38,940 And then once that happens, this if statement doesn't run anymore because brightness is equal to the 112 00:07:38,940 --> 00:07:41,180 target brightness. 113 00:07:41,180 --> 00:07:46,040 Now, we also need to do this for when our target brightness is zero, so we can check if the lighting 114 00:07:46,040 --> 00:07:50,240 service dot brightness is greater than our target brightness. 115 00:07:50,240 --> 00:07:52,910 So let's say it's at three and we need to set it to zero. 116 00:07:53,090 --> 00:07:55,670 Then we can do the same thing here. 117 00:07:55,670 --> 00:08:02,960 We're going to set it equal to math dot max, which returns the maximum number out of the numbers you 118 00:08:02,960 --> 00:08:04,100 pass to the function. 119 00:08:04,100 --> 00:08:09,590 And again, we're going to pass target brightness, which would be zero, and we're going to do lighting 120 00:08:09,590 --> 00:08:13,520 service, dot brightness, subtract it by this brightness delta. 121 00:08:14,100 --> 00:08:19,020 So just in case it subtracts more than it needs to and it sets the brightness to be negative, it'll 122 00:08:19,020 --> 00:08:20,250 set it back to zero. 123 00:08:20,280 --> 00:08:24,210 That way we never go below zero and we never go above three here. 124 00:08:25,490 --> 00:08:33,080 And then the last thing we need to check is if the time elapsed becomes greater than we can do, greater 125 00:08:33,080 --> 00:08:34,960 than or equal to the day length. 126 00:08:34,970 --> 00:08:44,060 So once we have surpassed 240 seconds, well, what we need to do is we need to set the start variable 127 00:08:44,090 --> 00:08:49,280 back to the current time and we can do that by just doing start is equal to tick. 128 00:08:50,570 --> 00:08:56,150 So now that we have reset the start time, when we go back to our time elapsed, it'll give us the correct 129 00:08:56,180 --> 00:08:56,840 time. 130 00:08:57,020 --> 00:09:00,260 So of course, tick minus, tick would be zero. 131 00:09:00,260 --> 00:09:05,870 So zero seconds have elapsed and we would continue elapsing the time as tick goes on. 132 00:09:06,050 --> 00:09:12,860 And then the last very last thing we need to do is we need to set the lighting service clock time to 133 00:09:12,860 --> 00:09:15,080 the hour of day and that's it. 134 00:09:15,110 --> 00:09:21,260 We have built a simple day night cycle, so let's go over it one more time just to make sure you understand 135 00:09:21,260 --> 00:09:23,210 everything in this constant. 136 00:09:23,210 --> 00:09:26,000 We're defining how long we want the day to be in seconds. 137 00:09:26,010 --> 00:09:27,770 It is 240 seconds. 138 00:09:27,800 --> 00:09:31,010 We're defining what we want the time to start at. 139 00:09:31,010 --> 00:09:37,280 So when the game first runs, we want the time to start at 4 a.m. This number defines us. 140 00:09:37,280 --> 00:09:42,950 The maximum brightness we'll need in the game, which we divide by the day length to give us a brightness 141 00:09:42,950 --> 00:09:47,870 delta that we use to increase and decrease the current brightness in our game. 142 00:09:48,170 --> 00:09:54,360 And actually, since this is the same number here, what we could do is we could just set this to max 143 00:09:54,360 --> 00:09:58,830 brightness and actually let's create another constant, we'll call it min brightness. 144 00:09:58,830 --> 00:10:04,620 So the minimum brightness, oops, brightness and we'll set it to zero. 145 00:10:05,200 --> 00:10:07,930 And then we can set that to the minimum brightness. 146 00:10:07,930 --> 00:10:11,380 That way we don't have any magic, random floating numbers around. 147 00:10:11,740 --> 00:10:16,010 So once we have these constants acquired, we go down and create a variable called START. 148 00:10:16,030 --> 00:10:22,620 And it's basically just the current time subtracted by our day length based on our current hour. 149 00:10:22,630 --> 00:10:26,380 So we get the fraction out of 24 hours of our start hour. 150 00:10:26,380 --> 00:10:32,620 So four divided by 24 and we multiply that to day length to give us the correct percentage or how many 151 00:10:32,620 --> 00:10:36,430 seconds of our day length is equal to 4/24. 152 00:10:36,880 --> 00:10:43,420 And then once we have this start, we go into our event here, we subtract tick by our current start 153 00:10:43,450 --> 00:10:43,750 time. 154 00:10:43,750 --> 00:10:50,200 So we know the time elapsed is 40 seconds and then we calculate the hour of day based on that, which 155 00:10:50,200 --> 00:10:52,380 would be four hours. 156 00:10:52,390 --> 00:10:56,170 And then we set the target brightness based on what time it is. 157 00:10:56,170 --> 00:11:00,130 So if it's in the morning, the target brightness is going to be three. 158 00:11:00,160 --> 00:11:03,690 If it's in the evening or at night time, then it'll be at zero. 159 00:11:03,700 --> 00:11:08,210 And based on that, we increase or decrease the brightness over time. 160 00:11:08,790 --> 00:11:14,730 And then once our time elapse becomes greater than or equal to our day length, then we reset the start 161 00:11:14,730 --> 00:11:17,310 back to tick and then tick. 162 00:11:17,310 --> 00:11:18,870 Divided by tick is zero. 163 00:11:18,870 --> 00:11:21,120 So that starts at a new day. 164 00:11:21,360 --> 00:11:26,730 And then finally, we set the clock time equal to the hour of day we calculated up here. 165 00:11:27,730 --> 00:11:34,000 So the last thing we need to do is to test to see if it works so we can go and run our game and we can 166 00:11:34,000 --> 00:11:35,080 check to see. 167 00:11:35,320 --> 00:11:36,280 Well, look at that. 168 00:11:36,280 --> 00:11:40,090 It's currently nighttime and let's go ahead and look at the moon. 169 00:11:40,850 --> 00:11:41,390 And there you go. 170 00:11:41,390 --> 00:11:43,820 You see, our moon is slowly moving. 171 00:11:44,240 --> 00:11:47,810 Once it reaches the horizon, we should see the brightness increase on our map. 172 00:11:48,090 --> 00:11:49,580 Well, actually, we can monitor it. 173 00:11:49,610 --> 00:11:51,080 Monitor it from here. 174 00:11:53,000 --> 00:11:55,790 So pretty soon our brightness should start to go up. 175 00:12:04,880 --> 00:12:05,330 There we go. 176 00:12:05,360 --> 00:12:06,800 Our brightness is going up. 177 00:12:08,790 --> 00:12:16,500 And now our son is going to slowly move until the total day or 24 hours is equal to 240 seconds. 178 00:12:16,500 --> 00:12:18,480 And then we restart and start a new day. 179 00:12:19,630 --> 00:12:22,060 So we can see our sun slowly moving there. 180 00:12:22,610 --> 00:12:27,710 Now, if we want to make the day faster, we can set it to something like 30 seconds. 181 00:12:28,770 --> 00:12:32,790 And we don't need to change anything else because remember, we're calculating everything here using 182 00:12:32,790 --> 00:12:33,120 math. 183 00:12:33,120 --> 00:12:35,900 There's no defined constants for the brightness delta. 184 00:12:35,910 --> 00:12:37,920 We don't have to mess with any of these variables. 185 00:12:37,920 --> 00:12:39,810 It's all based on our day length. 186 00:12:41,010 --> 00:12:42,780 So if we run the game again. 187 00:12:43,460 --> 00:12:47,240 We should see that it's definitely much faster. 188 00:12:48,520 --> 00:12:53,670 And that also means our brightness should go down quicker as well because our time is moving faster. 189 00:12:53,680 --> 00:12:55,780 So there we go, already at zero. 190 00:12:56,980 --> 00:12:58,200 And then the moon's up. 191 00:12:58,210 --> 00:12:59,530 It's coming around. 192 00:13:01,160 --> 00:13:07,370 Moon goes to the horizon and then the sun comes up and boom, we're back at three brightness. 193 00:13:08,280 --> 00:13:10,590 And it just continues this on and on. 194 00:13:10,950 --> 00:13:15,600 So as you can see, it's pretty simple to make a day night cycle in your game really only takes about 195 00:13:15,600 --> 00:13:16,940 38 lines of code. 196 00:13:16,950 --> 00:13:22,980 And in here, you can also continue to change other things in the lighting properties like you can change 197 00:13:22,980 --> 00:13:30,120 maybe the ambient color, you could try to change, maybe the diffuse and the specular numbers. 198 00:13:30,120 --> 00:13:35,310 You could try changing the color shift properties, kind of do miss around whatever you'd like in this 199 00:13:35,310 --> 00:13:36,030 event. 200 00:13:36,270 --> 00:13:39,150 So I hope you enjoyed it and I'll see you in the next lecture.